#include <avr/io.h>
//#include <util/delay.h>
#include "vs1001k.h"
//#include "spi.h"

#define READ 0x3
#define WRITE 0x2
#define XCS PINB

//MODE control
#define SM_DIFF		0	//Differential			[0] Normal in-phase audio	[1] left channel inverted
#define SM_FFWD		1	//Fast forward			[0] Normal playback 	 	[1] fast forward on
#define SM_RESET	2	//Soft reset			[0] No reset 		 	[1] reset
#define SM_UNUSED1	3	//Unused set to 0
#define SM_PDOWN	4	//Powerdown			[0] Power on			[1] Power off
#define SM_UNUSED2	5	//Unused set to 0
#define SM_UNUSED3	6	//Unused set to 0
#define SM_BASS		7	//Bass/treble enhancer		[0] off				[1] on	
#define SM_DACT		8	//DCLK active edge		[0] rising			[1] falling
#define SM_BITORD	9	//Byte order on SPI bus 	[0] MSB first			[1] MSB last
#define SM_IBMODE	10	//Input bus mode		[0] Slave			[1] Master
#define SM_IBCLK	11	//Input bus clk when master	[0] 512 kHz			[1] 1024 kHz
unsigned char mode_high;
unsigned char mode_low;
//[ 0 0 0 0 SM_IBCLK SMIBMODE SM_BITORD SM_DACT ] [ SM_BASS 0 0 SM_PDOWN 0 SM_RESET SM_FFWD SM_DIFF ]
//[ 0 0 0 0 0 0 0 0 ] [ 0 0 0 0 0 0 0 0 ] // 0x00 0x00
//[ 0 0 0 0 0 0 1 0 ] [ 1 0 0 0 0 0 0 0 ] // 0x02 0x80 - MSB Last, Bass/Treble enhancer
//[ 0 0 0 0 0 0

//The initiazing of the vs1001k chip
void VS1001k_init()
{
 mode_high = 0x00;
 mode_low = 0x00;
}
//Sound test in the vs1001k chip
// N has to between 48 and 119
void VSSoundTest(unsigned char channel, unsigned char N)
{ 
 unsigned char idx;
// unsigned char Send;
 unsigned char SoundSekvens[8]; 

 SoundSekvens[0] = 0x53;	// Sekvens for at lave lydtest
 SoundSekvens[1] = 0xEF;	//
 SoundSekvens[2] = 0x6E;	//

 if((N < 119) && (N > 48))
	SoundSekvens[3] = N;
 else
	SoundSekvens[3] = 0x30; // Den er 48 i decimal værdi

 SoundSekvens[4] = 0;		//
 SoundSekvens[5] = 0;		//
 SoundSekvens[6] = 0;		//
 SoundSekvens[7] = 0;		//

 if(channel == LEFT) 
 {
   SoundSekvens[7] = 0;
 }
 
 if(channel == RIGHT)
 {
   SoundSekvens[7] = 0;
 }
 
 if(channel == BOTH)
 {
   SoundSekvens[7] = 0;
 }

 for(idx = 0; idx <= 7; idx++)
 {
  SoundSekvens[idx];
 }
 
}

/*
  Memory test: Der kan ikke interuptes på vs1001k chip mens den kør.
  |-----|----------------------------|
  | Bit | Betyder                    |
  |-----|----------------------------|
  | 0   | God X ROM                  |
  | 1   | God Y ROM (Høj)            |  
  | 2   | God Y ROM (Lav)            |
  | 3   | God Y RAM                  |
  | 4   | God X RAM                  |
  | 5   | God instruktion RAM (Høj)  |
  | 6   | God instruktion RAM (Lav)  |
  | 7   | Bruges ikke                |
  |-----|----------------------------|

  Det vil sige hvis man får X111 1111 = 0xEF så er det hele godt :)
  Men hvis man får X000 1111 = 0x0F Så Den gal med rammene :(

*/
unsigned char VSMemTest()
{
 unsigned char idx;
 unsigned char Value;
 unsigned char MemTestData[8];
 
 MemTestData[0] = 0x4D;
 MemTestData[1] = 0xEA;
 MemTestData[2] = 0x6D;
 MemTestData[3] = 0x54;
 MemTestData[4] = 0;
 MemTestData[5] = 0;
 MemTestData[6] = 0;
 MemTestData[7] = 0;
 
 //Sender Memory test sekvens
 for(idx = 0; idx <= 7; idx++)
 {
  MemTestData[idx];
 }
 //Delay 500000 clock cycles
 //Value = VSwrite(0);

 return Value;
}

void FastForward()
{
 mode_low ^= (1<<SM_FFWD);
}

void Differential()
{
 mode_low ^= (1<<SM_DIFF);
}

void SoftReset()
{
 mode_low ^= (1<<SM_RESET);
}

void PowerDown()
{
 mode_low ^= (1<<SM_PDOWN);
}

void BassTreble()
{
 mode_low ^= (1<<SM_BASS);
}

void DclkActive()
{
 mode_high ^= (1<<(SM_DACT-8));
}

void BitOrder()
{
 mode_high ^= (1<<(SM_BITORD-8));
}

//Writing og reading protocol is that:
// 1 instruction byte
// 1 address byte
// 16 bit data word
unsigned char VSread(unsigned char addr, unsigned char byte_high, unsigned char byte_low)
{
 XCS = 0; //Select device
 SPITransmit(READ);
 SPITransmit(addr);
 SPITransmit(byte_high);
 SPITransmit(byte_low);
 return 0;
}

unsigned char VSwrite(unsigned char addr, unsigned char byte_high, unsigned char byte_low)
{
 XCS = 0; //Select device
 SPITransmit(WRITE);
 SPITransmit(addr);
 SPITransmit(byte_high);
 SPITransmit(byte_low); 
 return 0;
}


//Sending a command to the vs1001k chip
/*unsigned char VSSendCmd(unsigned char cmd) 
{
 return SPITransmit(cmd);
}

//Sending the mp3 file data to the vs1001k chip
unsigned char VSSendData(unsigned char data) 
{
 return SPITransmit(data);
}*/


